package com.hnzq.cloud.freshuserassembly.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.hnzq.cloud.RoleAPI;
import com.hnzq.cloud.dto.PageRolesDto;
import com.hnzq.cloud.dto.RoleDto;
import com.hnzq.cloud.freshuserassembly.service.IFreRoleService;
import com.hnzq.cloud.model.Result;
import com.hnzq.cloud.param.AddRoleParam;
import com.hnzq.cloud.param.DeleteRoleParam;
import com.hnzq.cloud.param.FindRolesParam;
import com.hnzq.cloud.param.UpdateRoleParam;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 周皓月
 * @since 2022-02-03
 */
@RestController
@RequestMapping("/freRole")
public class FreRoleController implements RoleAPI {

    @Autowired
    private IFreRoleService frs;

    /**
     * 分页条件查询角色
     * @param findRolesParam
     * @return
     */
    @Override
    public Result<PageRolesDto> findRolesByCondition(FindRolesParam findRolesParam) {
        return Result.newInstance().setMessage("查询成功").setData(frs.findRolesByCondition(findRolesParam));
    }

    /**
     * 新增角色
     * @param addRoleParam
     * @return
     */
    @Override
    @GlobalTransactional
    public Result<RoleDto> addRole(AddRoleParam addRoleParam)  {
        return Result.newInstance().setMessage("新增成功").setData(frs.addRole(addRoleParam));
    }

    /**
     * 更新角色
     * @param updateRoleParam
     * @return
     */
    @Override
    @GlobalTransactional
    public Result<RoleDto> updateRoleById(UpdateRoleParam updateRoleParam) throws JsonProcessingException {
        return Result.newInstance().setMessage("更新成功").setData(frs.updateRoleById(updateRoleParam));
    }

    /**
     * 删除角色
     * @param deleteRoleParam
     * @return
     */
    @Override
    @GlobalTransactional
    public Result<Boolean> deleteRoleById(DeleteRoleParam deleteRoleParam) throws JsonProcessingException {
        if (frs.deleteRoleById(deleteRoleParam)){
            return Result.newInstance().setMessage("删除成功");
        }
        return Result.newInstance().setCode(500).setMessage("删除失败");
    }


}

